package com.zelin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zelin.mapper.ClassesMapper;
import com.zelin.mapper.StudentMapper;
import com.zelin.pojo.Classes;
import com.zelin.pojo.Student;
import com.zelin.pojo.StudentExample;
import com.zelin.service.StudentService;
import com.zelin.util.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: Feng.Wang
 * @Company: Zelin.ShenZhen
 * @Description:
 * @Date: Create in 2019/4/10 12:13
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    /**
     * 查询所有的学生
     * @return
     */
    @Override
    public List<Student> findAll() {
        List<Student> students = studentMapper.selectByExample(null);
        for (Student student : students) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
        return students;
    }

    /**
     * 分页查询，不带条件查询功能
     * @param page
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<Student> findByPage(int page, int pagesize) {
        //1.开始分页
        PageHelper.startPage(page,pagesize);
        //2.进行查询得到Page对象
        //2.1)创建查询实例
        StudentExample example = new StudentExample();
        //2.2)根据查询实例得到Page对象
       Page<Student> pages = (Page<Student>) studentMapper.selectByExample(example);
       //2.3)重新设置班级名称
        for (Student student : pages.getResult()) {
            Classes classes = classesMapper.selectByPrimaryKey(student.getCid());
            student.setCname(classes.getCname());
        }
       //3.返回查询到的结果
        return new PageResult<>(pages.getTotal(),pages.getResult());
    }

    /**
     * 条件查询带分页
     * @param page
     * @param pagesize
     * @param student
     * @return
     */
    @Override
    public PageResult<Student> search(int page, int pagesize, Student student) {
        //1.开始分页
        PageHelper.startPage(page,pagesize);
        //2.进行查询得到Page对象
        //2.1)创建查询实例
        StudentExample example = new StudentExample();
        //2.2)封装查询条件
        StudentExample.Criteria criteria = example.createCriteria();
        //① 将查询条件添加到example查询实例中
        if(student != null){
            if(StringUtils.isNotEmpty(student.getSname())){
                criteria.andSnameLike("%" + student.getSname() + "%");
            }
            if(StringUtils.isNotEmpty(student.getAddr())){
                criteria.andAddrLike("%" + student.getAddr() + "%");
            }
            if(student.getCid() != null && student.getCid().intValue() != 0){
                criteria.andCidEqualTo(student.getCid());
            }
        }
        //2.3)根据查询实例得到Page对象
        Page<Student> pages = (Page<Student>) studentMapper.selectByExample(example);
        //2.4)重新设置班级名称
        for (Student st : pages.getResult()) {
            Classes classes = classesMapper.selectByPrimaryKey(st.getCid());
            st.setCname(classes.getCname());
        }
        return new PageResult<>(pages.getTotal(),pages.getResult());
    }

    /**
     * 添加学生
     * @param student
     */
    @Override
    public void add(Student student) {
        studentMapper.insert(student);
    }

    /**
     * 修改学生
     * @param student
     */
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    /**
     * 根据学生编号删除学生
     * @param sid
     */
    @Override
    public void delete(String sid) {
        studentMapper.deleteByPrimaryKey(Integer.parseInt(sid));
    }

    /**
     * 批量删除学生
     * @param ids
     */
    @Override
    public void batchDel(String[] ids) {
        for (String id : ids) {
            studentMapper.deleteByPrimaryKey(Integer.valueOf(id));
        }
    }
}
